package com.xywy.bloodandroid.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BasicAdapter;
import com.xywy.bloodandroid.bean.OnlineQuestAnswerData;
import com.xywy.bloodandroid.util.ImageLoader.ImageCallback;

/**
 * 在线问题页面，医生回答的适配器
 * 
 * @author yangyu
 */
public class QuestOnlineAnswerAdapter extends
		BasicAdapter<OnlineQuestAnswerData> {
	private Context context;
	private List<OnlineQuestAnswerData> list = new ArrayList<OnlineQuestAnswerData>();
	private LayoutInflater inflater;
	private ListView lv;

	public QuestOnlineAnswerAdapter(Context context,
			List<OnlineQuestAnswerData> list, ListView lv) {
		super(context, list);
		this.context = context;
		this.list = list;
		this.lv = lv;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		viewholder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_online_answer, null);
			holder = new viewholder();
			holder.doctor_name = (TextView) convertView
					.findViewById(R.id.doctor_name);
			holder.doctor_academic = (TextView) convertView
					.findViewById(R.id.doctor_academic);
			holder.hosptail_name = (TextView) convertView
					.findViewById(R.id.hosptail_name);
			holder.recollectionss = (TextView) convertView
					.findViewById(R.id.recollectionss);
			holder.doctor_time = (TextView) convertView
					.findViewById(R.id.doctor_time);
			holder.question_type = (TextView) convertView
					.findViewById(R.id.question_type);
			holder.doctot_suggest = (TextView) convertView
					.findViewById(R.id.doctot_suggest);
			holder.iv_avatar = (ImageView) convertView
					.findViewById(R.id.iv_avatar);
			convertView.setTag(holder);
		} else {
			holder = (viewholder) convertView.getTag();
		}
		

		/* 展示数据 */
		holder.doctor_name.setText(list.get(position).getDocName());
		holder.doctor_academic.setText(list.get(position).getDocLevel());
		holder.hosptail_name.setText(list.get(position).getHospital());
		holder.recollectionss.setText(""); // 返回的字段中没有

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		holder.doctor_time.setText("发表于："
				+ sdf.format(new Date(list.get(position).getRepTime() * 1000)));

		String[] content = list.get(position).getDocRepliedContent()
				.split("\\*\\*\\*\\*");

		if (content[0] != null) {
			int length = content[0].length();
			String result = content[0].substring(5, length);
			holder.question_type.setText(result);
		}
		if (content[1] != null) {
			int length = content[1].length();
			String result = content[1].substring(5, length);
			holder.doctot_suggest.setText(result);
		}

		holder.iv_avatar.setVisibility(View.GONE);

		Drawable cachedImage = loader.loadDrawable(list.get(position)
				.getDocAvatar(), holder.iv_avatar, new ImageCallback() {

			@Override
			public void imageLoaded(Drawable imageDrawable,
					ImageView imageView, String imageUrl) {
				imageView = (ImageView) lv.findViewWithTag(imageUrl);
				if (imageView != null && imageDrawable != null) {
					imageView.setBackgroundDrawable(imageDrawable);
				}
			}
		});

		if (cachedImage == null) {
			holder.iv_avatar.setBackgroundResource(R.drawable.head);
		} else {
			holder.iv_avatar.setBackgroundDrawable(cachedImage);
		}

		return convertView;
	}

	private class viewholder {
		RelativeLayout doctor_rel; // 医生布局
		TextView doctor_name; // 医生姓名
		TextView doctor_academic; // 医生职称
		TextView hosptail_name; // 医院名称
		TextView recollectionss; // 科室？
		TextView doctor_time; // 医生时间
		TextView question_type; // 医生分析
		TextView doctot_suggest; // 医生建议
		ImageView iv_avatar;

	}

}
